﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GabageFactory.Models;

namespace GabageFactory.Controllers
{
    [Authorize]
    public class TailingpondController : BaseController
    {
        private FactoryReposity factoryReposity;

        private TailingpondReposity tailingpondReposity;

        public TailingpondController()
        {
            factoryReposity = new FactoryReposity();
            tailingpondReposity = new TailingpondReposity();
        }
        
        //
        // GET: /Tailingpond/

        public ActionResult Index(int? factoryId, string name)
        {
            ActionResult result = AllowForRole(RoleEnum.Administrator);
            if (result != null)
            {
                return result;
            }
            int id = 0;
            if (factoryId.HasValue)
            {
                id = factoryId.Value;
            }
            ShowFactoryList(id);
            return View(tailingpondReposity.QueryTailingponds(id, name));
        }

        //
        // GET: /Factory/Create

        public ActionResult Create()
        {
            ActionResult result = AllowForRole(RoleEnum.Administrator);
            if (result != null)
            {
                return result;
            }

            ShowFactoryList(0);
            return View(new Tailingpond());
        } 

        //
        // POST: /Factory/Create

        [HttpPost]
        public ActionResult Create(Tailingpond tailingpond)
        {
            ActionResult result = AllowForRole(RoleEnum.Administrator);
            if (result != null)
            {
                return result;
            }
            try
            {
                tailingpond = tailingpondReposity.CreateTailingpond(tailingpond);

                return RedirectToAction("Index");
            }
            catch
            {
                ShowFactoryList(tailingpond.FactoryId);
                return View(tailingpond);
            }
        }
        
        //
        // GET: /Tailingpond/Edit/5
 
        public ActionResult Edit(int id)
        {
            ActionResult result = AllowForRole(RoleEnum.Administrator);
            if (result != null)
            {
                return result;
            }
            Tailingpond tailingpond = tailingpondReposity.GetTailingpondById(id);

            ShowFactoryList(tailingpond.FactoryId);
            return View(tailingpond);
        }

        //
        // POST: /Tailingpond/Edit/5

        [HttpPost]
        public ActionResult Edit(Tailingpond tailingpond)
        {
            ActionResult result = AllowForRole(RoleEnum.Administrator);
            if (result != null)
            {
                return result;
            }
            try
            {
                validate(tailingpond);

                Tailingpond oldTailingpond = tailingpondReposity.GetTailingpondById(tailingpond.Id);
                oldTailingpond.Name = tailingpond.Name;
                oldTailingpond.Description = tailingpond.Description;
                oldTailingpond.FactoryId = tailingpond.FactoryId;
                oldTailingpond.ModifyTime = DateTime.Now;
                tailingpondReposity.EditTailingpond(oldTailingpond);

                return RedirectToAction("Index");
            }
            catch
            {
                ShowFactoryList(tailingpond.FactoryId);
                
                return View(tailingpond);
            }
        }

        //
        // GET: /Factory/Delete/5
 
        public ActionResult Delete(int id)
        {
            ActionResult result = AllowForRole(RoleEnum.Administrator);
            if (result != null)
            {
                return result;
            }
            Tailingpond tailingpond = tailingpondReposity.GetTailingpondById(id);
            ViewData["Name"] = tailingpond.Name;
            tailingpondReposity.DeleteTailingpond(tailingpond);
            
            return View();
        }


        //
        // GET: /Stack/Details/5

        public ActionResult Details(int id)
        {
            List<RoleEnum> roles = new List<RoleEnum>();
            roles.Add(RoleEnum.Auditor);
            roles.Add(RoleEnum.Manager);
            ActionResult result = AllowForRole(roles);
            if (result != null)
            {
                return result;
            }
            Tailingpond tailingpond = tailingpondReposity.GetTailingpondById(id);
            return View(tailingpond);
        }


        private void validate(Tailingpond tailingpond)
        {
            if (string.IsNullOrEmpty(tailingpond.Name))
            {
                ModelState.AddModelError("Name", "尾矿库名称不能为空");
            }
            else if (tailingpond.Name.Length > 128)
            {
                ModelState.AddModelError("Name", "尾矿库名称不能超过128个字符");
            }

            if (string.IsNullOrEmpty(tailingpond.Description))
            {
                ModelState.AddModelError("Description", "尾矿库描述不能为空");
            }
            else if (tailingpond.Description.Length > 2000)
            {
                ModelState.AddModelError("Description", "尾矿库名称不能超过2000个字符");
            }

            if (!ModelState.IsValid)
            {
                throw new Exception("Input form is invalid!");
            }
        }
    }
}
